package com.utils;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.session.Session;

import java.io.*;

public class SerializableUtils {
    public static String serialize(Session session) {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos);) {
            oos.writeObject(session);
            return Base64.encodeToString(bos.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Session deSerialize(String sessionStr) {
        try (ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(sessionStr));
             ObjectInputStream ois = new ObjectInputStream(bis);) {
            return (Session) ois.readObject();
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
